/*
 * Copyright 2003-2006 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

%{
	void print_traceback()
	{
        PyObject *err_type, *err_value, *err_traceback;
		PyObject *result, *moduleTb, *fmt, *list, *s;

        int have_error = PyErr_Occurred() != 0;

		if (have_error)
			PyErr_Fetch(&err_type, &err_value, &err_traceback);

		moduleTb = PyImport_ImportModule("traceback");
		fmt = PyObject_GetAttrString(moduleTb, "format_exception");

		if (have_error)
			PyErr_Restore(err_type, err_value, err_traceback);

        have_error = (PyErr_Occurred() != NULL);

		list = PyObject_CallFunction(fmt, "(OOO)", err_type, err_value, err_traceback );
		s = PyString_FromString("");
		result = PyObject_CallMethod(s, "join", "(O)", list);
		
		printf( "Error in python callback:\n\n" );
		if (result)
		{
			printf( PyString_AsString(result) );
		}
		else
		{
			printf( "No traceback available!" );
		}

        Py_XDECREF(result);
        Py_XDECREF(s);
        Py_XDECREF(list);
        Py_XDECREF(fmt);
        Py_XDECREF(moduleTb);
    }


	PyObject* _validate_callable(void* data)
	{
		PyObject* callable = (PyObject*)data;
		
		if ( !data )
		{
			printf( "No python callback (zero pointer)!\n" );
			return 0;
		}
		
		if ( !PyCallable_Check(callable) )
		{
			printf( "The python callback is not callable!\n" );
			return 0;
		}
		
		return callable;
	}
%}